// 获取全局应用程序实例对象
const app = getApp()
	// 创建页面实例对象
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		title: 'orderReserve',
		buyTickets: [],
		totalSum: 0,
		choiceTicket: [],
		clubId: "",
		findDate: "",
		type: ""
	},

	/**
	 * 获取可供选择的票券
	 */
	getBuyTickets() {
		app.wechat.getStorage('orderReserve')
			.then(res => {
				wx.setNavigationBarTitle({
				  title:  res.data.choicetype.stName +'票预订'
				})
				let date = new Date();
				let findDate = date.getFullYear() + "-" + res.data.reserveItem.month + "-" + res.data.reserveItem.strDate;
				let type = res.data.choicetype.stCode == "" ? "swim" : res.data.choicetype.stCode;
				let stName = res.data.choicetype.stName;
				this.setData({ clubId: res.data.reserveItem.clubId });
				this.setData({ findDate: findDate });
				this.setData({ type: type });
				app.yds.getBuyTickets(res.data.reserveItem.clubId, findDate, type)
					.then(res => {
						if (res.code && res.code == 0) {
							if (res.data.length == 0) {
								wx.showToast({
									title: stName + '暂无票可以订购,非常抱歉!',
									icon: 'loading',
									duration: 3000
								})
								return;
							}
							this.setData({ buyTickets: res.data });
							//初始化数据
							for (var i = 0; i < this.data.buyTickets.length; i++) {
								this.data.buyTickets[i]["choiceNum"] = 0;
								this.data.buyTickets[i]["choiceSum"] = 0;
							}
							this.setData({ buyTickets: this.data.buyTickets });

						} else {
							app.wechat.failTotal(res.msg)
						}

					});
			});
		wx.hideToast();
	},

	clickRelease(e) {
		let ticketId = e.target.dataset.choiceticket.id;
		this.caculateReleaseSingleMoney(ticketId);

	},

	clickAdd(e) {
		let ticketId = e.target.dataset.choiceticket.id;
		this.caculateAddSingleMoney(ticketId);
	},

	caculateAddSingleMoney(ticketId) {
		for (var i = 0; i < this.data.buyTickets.length; i++) {
			if (this.data.buyTickets[i].id == ticketId) {
				this.data.buyTickets[i]["choiceNum"] = this.data.buyTickets[i].choiceNum + 1;
				this.data.buyTickets[i]["choiceSum"] = Number(this.data.buyTickets[i].price) * Number(this.data.buyTickets[i].choiceNum);
				this.caculateChoiceTicket(this.data.buyTickets[i], 0);

			}
		}
		this.setData({ buyTickets: this.data.buyTickets });
		this.caculateTotalMoney();
	},

	caculateReleaseSingleMoney(ticketId) {
		for (var i = 0; i < this.data.buyTickets.length; i++) {
			if (this.data.buyTickets[i].id == ticketId) {
				if (this.data.buyTickets[i].choiceNum == 0) {
					this.caculateChoiceTicket(this.data.buyTickets[i], 1);
					return;
				}
				this.data.buyTickets[i]["choiceNum"] = this.data.buyTickets[i].choiceNum - 1;
				this.data.buyTickets[i]["choiceSum"] = Number(this.data.buyTickets[i].price) * Number(this.data.buyTickets[i].choiceNum);
			}
		}
		this.setData({ buyTickets: this.data.buyTickets });
		this.caculateTotalMoney();
	},

	caculateTotalMoney() {
		this.setData({ totalSum: 0 });
		for (var i = 0; i < this.data.buyTickets.length; i++) {
			this.data.totalSum = this.data.totalSum + this.data.buyTickets[i].choiceSum;
		}
		this.setData({ totalSum: getApp().util.returnFloat(this.data.totalSum) });
		this.caculateChoiceTicket();

	},

	caculateChoiceTicket() {
		this.data.choiceTicket = [];
		for (var i = 0; i < this.data.buyTickets.length; i++) {
			if (this.data.buyTickets[i].choiceNum > 0) {
				this.data.choiceTicket.unshift(this.data.buyTickets[i]);
			}
		}
		this.setData({ choiceTicket: this.data.choiceTicket });
	},

	/**
	 * 点击支付进入支付页面(生成订单再进入支付环节)
	 */
	clickOrderPay(event) {

		if(!this.data.choiceTicket.length){
			this.wetoast.toast({
			    duration: 2000,
			    title: '购票不能为空',
			    img: '../../images/warn.png'
			})
			return;
		}

		var goodsDetails = [];
		for (var i = 0; i < this.data.choiceTicket.length; i++) {
			goodsDetails.push({ tkTypeId: this.data.choiceTicket[i].id, num: this.data.choiceTicket[i].choiceNum });
		}
		let j = { "goodsDetails": goodsDetails, "couponNo": "", "date": this.data.findDate };
		let clubID = this.data.clubId;
		let orderPayParams = {
			totalSum: this.data.totalSum,
			choiceTicket: this.data.choiceTicket,
			findDate: this.data.findDate,
			type: this.data.type,
			clubId: this.data.clubId,
			postj: j
		};
		app.wechat.setStorage('orderPayParams', orderPayParams)
			.then(res => {
				wx.navigateTo({
					url: '../orderPay/orderPay'
				})
			});

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad() {
		// TODO: onLoad
		new app.WeToast();
		this.getBuyTickets();
		//app.wechat.setStorage

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {
		// TODO: onReady
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		// TODO: onShow
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {
		// TODO: onHide
	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {
		// TODO: onUnload
	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {
		// TODO: onPullDownRefresh
	}
})
